


#ifndef PF_POLYLINE_H
#define PF_POLYLINE_H

#include "pf_entity.h"
#include "pf_entitycontainer.h"
#include "lx_Export.h"


/**
 * Holds the data that defines a polyline.
 */
class LX_EXPORT PF_PolylineAttribute : public PF_Flags {
public:
    PF_PolylineAttribute() {
        startpoint = endpoint = PF_Vector(false);
    }
    PF_PolylineAttribute(const PF_Vector& startpoint,
                    const PF_Vector& endpoint,
                    bool closed) {

        this->startpoint = startpoint;
        this->endpoint = endpoint;
        if (closed==true) {
            setFlag(LX_Define::FlagClosed);
        }
    }

    friend class PF_Polyline;

    friend std::ostream& operator << (std::ostream& os,
                                      const PF_PolylineAttribute& pd) {
        os << "(" << pd.startpoint <<
        "/" << pd.endpoint <<
        ")";
        return os;
    }

private:
    PF_Vector startpoint;
    PF_Vector endpoint;
};



/**
 * Class for a poly line entity (lots of connected lines and arcs).
 *

 */
class LX_EXPORT PF_Polyline : public PF_Container {
public:
    PF_Polyline(PF_Container* parent=NULL);
    PF_Polyline(PF_Container* parent,
                const PF_PolylineAttribute& d);
    virtual ~PF_Polyline();

    virtual PF_Entity* clone() {
        PF_Polyline* p = new PF_Polyline(*this);
        p->setOwner(isOwner());
        p->initId();
        p->detach();
        return p;
    }

    /**	@return LX_Define::EntityPolyline */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityPolyline;
    }

    /** @return Copy of data that defines the polyline. */
    PF_PolylineAttribute getData() const {
        return data;
    }

    /** sets a new start point of the polyline */
    void setStartpoint(PF_Vector& v) {
        data.startpoint = v;
        if (!data.endpoint.valid) {
            data.endpoint = v;
        }
    }

    /** @return Start point of the entity */
    PF_Vector getStartpoint() {
        return data.startpoint;
    }
	
    /** sets a new end point of the polyline */
    void setEndpoint(PF_Vector& v) {
        data.endpoint = v;
    }

    /** @return End point of the entity */
    PF_Vector getEndpoint() {
        return data.endpoint;
    }

	double getClosingBulge();

	void updateEndpoints();

    /** @return true if the polyline is closed. false otherwise */
    bool isClosed() const {
        return data.getFlag(LX_Define::FlagClosed);
    }

	void setClosed(bool cl) {
		if (cl) {
			data.setFlag(LX_Define::FlagClosed);
		}
		else {
			data.delFlag(LX_Define::FlagClosed);
		}
	}
    
	virtual PF_VectorSolutions getRefPoints();
    virtual PF_Vector getNearestRef(const PF_Vector& coord,
                                     double* dist = NULL);
    virtual PF_Vector getNearestSelectedRef(const PF_Vector& coord,
                                     double* dist = NULL);

    virtual PF_Entity* addVertex(const PF_Vector& v, 
		double bulge=0.0, bool prepend=false);
	virtual void setNextBulge(double bulge) {
		nextBulge = bulge;
	}
    virtual void addEntity(PF_Entity* entity);
    //virtual void addSegment(PF_Entity* entity);
    virtual void removeLastVertex();
    virtual void endPolyline();

	//virtual void reorder();

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
	virtual void moveRef(const PF_Vector& ref, const PF_Vector& offset);
    virtual void stretch(PF_Vector firstCorner,
                         PF_Vector secondCorner,
                         PF_Vector offset);
	
    virtual void draw(PF_Painter* painter, PF_GraphicView* view, 
		double patternOffset=0.0);

    friend std::ostream& operator << (std::ostream& os, const PF_Polyline& l);

protected:
    virtual PF_Entity* createVertex(const PF_Vector& v, 
		double bulge=0.0, bool prepend=false);

protected:
    PF_PolylineAttribute data;
    PF_Entity* closingEntity;
	double nextBulge;
};

#endif
